package net.stuffrepos.bricknetcenter.util.validation;

/**
 *
 * @author Eduardo H. Bogoni <eduardobogoni@gmail.com>
 */
public class IntegerValidator implements Constraint<Integer> {

    private final int minValue;
    private final int maxValue;
    private final boolean required;

    public IntegerValidator(boolean required, Integer minValue, Integer maxValue) {
        this.required = required;
        this.minValue = (minValue == null ? Integer.MIN_VALUE : minValue);
        this.maxValue = (maxValue == null ? Integer.MAX_VALUE : maxValue);        
    }

    @Override
    public boolean isValid(Integer value) {
        if (required && value == null) {
            return false;
        }

        if (value < minValue) {
            return false;
        }

        if (value > maxValue) {
            return false;
        }

        return true;
    }
}
